<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="模板" prop="fullName">
        <el-select v-model="dataForm.tplId" class="m-2" placeholder="Select" size="large">
          <el-option v-for="item in exhtplList" :key="item.id" :label="item.shortName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="全称" prop="fullName">
        <el-input v-model="dataForm.fullName" placeholder="全称"></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker v-model="dataForm.startTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择开始时间" />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker v-model="dataForm.endTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择结束时间" />
      </el-form-item>
      <el-form-item label="订购结束时间" prop="orderEndTime">
        <el-date-picker v-model="dataForm.orderEndTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择订购结束时间" />
      </el-form-item>
      <el-form-item label="展会状态" prop="status">
        <el-switch v-model="dataForm.status" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item label="介绍" prop="description">
        <editor v-model="dataForm.description" :init="{ height: 300 }"></editor>
      </el-form-item>
      <el-form-item label="" prop="descriptionMobile">
        <el-input v-model="dataForm.descriptionMobile" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
import Tinymce from "@/components/tinymce";
export default defineComponent({
  components: { editor: Tinymce },
  setup() {
    interface Book {
      id: number;
      shortName: string;
    }
    let exhtplList = reactive<Book[]>([]);
    baseService.get("/noc/texhtpl/list").then((res) => {
      if (res.code === 0) {
        exhtplList.push(...res.data);
      }
    });
    return reactive({
      visible: false,
      exhtplList,
      dataForm: {
        id: "",
        tplId: "",
        fullName: "",
        startTime: "",
        endTime: "",
        orderEndTime: "",
        status: undefined,
        description: "",
        descriptionMobile: ""
      }
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/noc/texh/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/noc/texh", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>
